/*
 * Copyright (c) 2018-2999 湖南洛云网络科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.multishop.controller;

import cn.hutool.core.lang.Snowflake;
import com.yami.shop.bean.enums.BackType;
import com.yami.shop.bean.model.SmsPackage;
import com.yami.shop.bean.model.SmsRechargeLog;
import com.yami.shop.bean.pay.PayInfoDto;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.security.multishop.util.SecurityUtils;
import com.yami.shop.service.PayManagerService;
import com.yami.shop.service.SmsPackageService;
import com.yami.shop.service.SmsRechargeLogService;
import com.yami.shop.user.comment.param.PayUserParam;
import com.yami.shop.user.comment.service.UserLevelLogService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Objects;

/**
 * @author LHD on 2020/07/03.
 */
@Api(tags = "短信购买接口")
@RestController
@RequestMapping("/shop/sms")
@AllArgsConstructor
public class PaySmsController {

    /**
     * 会员信息
     */
    private final Snowflake snowflake;
    private final UserLevelLogService userLevelLogService;
    private final PayManagerService payManagerService;
    private final SmsPackageService smsPackageService;
    private final SmsRechargeLogService smsRechargeLogService;

    /**
     * 会员支付接口
     */
    @PostMapping("/paySms")
    @SneakyThrows
    public ResponseEntity<?> payVip(HttpServletResponse httpResponse, @RequestBody PayUserParam paySmsParam) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        SmsPackage smsPackage = smsPackageService.getById(paySmsParam.getId());
        if(Objects.isNull(smsPackage) || smsPackage.getIsDelete() == 1){
            // 购买的短信套餐不存在，请刷新后重试！
            throw new YamiShopBindException("yami.sys.sms.package");
        }
        // 使用雪花算法生成支付单号
        String payNo = String.valueOf(snowflake.nextId());
        Date now = new Date();
        //生成短信充值日志
        SmsRechargeLog smsRechargeLog = new SmsRechargeLog();
        smsRechargeLog.setCreateTime(now);
        smsRechargeLog.setShopId(shopId);
        smsRechargeLog.setPayNo(payNo);
        smsRechargeLog.setIsPayed(0);
        smsRechargeLog.setSmsNum(smsPackage.getSmsNum());
        smsRechargeLog.setSmsPackageId(smsPackage.getSmsPackageId());
        smsRechargeLog.setPayType(paySmsParam.getPayType());
        smsRechargeLog.setCostAmount(smsPackage.getAmount());
        //插入日志
        smsRechargeLogService.save(smsRechargeLog);

        PayInfoDto payInfo = new PayInfoDto();
        payInfo.setBody("商家短信购买");
        payInfo.setPayAmount(smsRechargeLog.getCostAmount());
        payInfo.setPayNo(payNo);
        payInfo.setBackType(BackType.SHOP.value());
        payInfo.setPayType(paySmsParam.getPayType());
        payInfo.setApiNoticeUrl("/shop/paySms/paySmsCallback/" + paySmsParam.getPayType());
        payInfo.setReturnUrl(paySmsParam.getReturnUrl());

        return payManagerService.doPay(httpResponse, payInfo);

//       // TODO 支付成功测试
//        smsRechargeLogService.paySuccess(payNo);
//        return ResponseEntity.ok().build();
    }

}
